package com.stylefeng.guns.modular.salesmanagement.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.modular.dto.CustomerReturnGoodsAddRequest;
import com.stylefeng.guns.modular.dto.CustomerReturnGoodsQueryRequest;
import com.stylefeng.guns.modular.system.model.GoodsSale;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.CustomerReturn;
import com.stylefeng.guns.modular.salesmanagement.service.ICustomerReturnService;

import java.util.Date;

/**
 * 客户退货控制器
 *
 * @author LiuXin
 * @Date 2018-09-12 21:21:45
 */
@Controller
@RequestMapping("/customerReturn")
public class CustomerReturnController extends BaseController {

    private String PREFIX = "/salesmanagement/customerReturn/";

    @Autowired
    private ICustomerReturnService customerReturnService;

    /**
     * 跳转到客户退货首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String returnSaleOrderNumber = "CTH" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        model.addAttribute("returnSaleOrderNumber", returnSaleOrderNumber);
        return PREFIX + "customerReturn.html";
    }

    /**
     * 跳转到添加客户退货
     */
    @RequestMapping("/customerReturn_add")
    public String customerReturnAdd() {
        return PREFIX + "customerReturn_add.html";
    }

    /**
     * 跳转到修改客户退货
     */
    @RequestMapping("/customerReturn_update/{customerReturnId}")
    public String customerReturnUpdate(@PathVariable Integer customerReturnId, Model model) {
        CustomerReturn customerReturn = customerReturnService.selectById(customerReturnId);
        model.addAttribute("item",customerReturn);
        LogObjectHolder.me().set(customerReturn);
        return PREFIX + "customerReturn_edit.html";
    }

    /**
     * 获取客户退货列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(CustomerReturnGoodsQueryRequest request) {
        Page<CustomerReturn> page = new PageFactory<CustomerReturn>().defaultPage();
        Page<CustomerReturn> resultPage = customerReturnService.selectPageWithDataScope(page,request);
        PageInfoBT<CustomerReturn> pageInfoBT = this.packForBT(resultPage);
        return pageInfoBT;
    }

    /**
     * 新增客户退货
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody CustomerReturnGoodsAddRequest customerReturn) {
        boolean success = customerReturnService.insertCustomerReturnListAndGoods(customerReturn);
        if(success){
            String returnSaleOrderNumber = "CTH" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
            return SUCCESS_TIP.setResultData(returnSaleOrderNumber);
        }
        return new ErrorTip(500, "保存失败! 请检查参数.");
    }

    /**
     * 删除客户退货
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam("customerReturnListId") Long customerReturnId) {
        boolean success = customerReturnService.deleteCustomerReturnListAndGoods(customerReturnId);
        if(success){
            return SUCCESS_TIP;
        }
        return new ErrorTip(500, "删除失败! 请稍后再试.");
    }

    /**
     * 修改客户退货
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CustomerReturn customerReturn) {
        customerReturnService.updateById(customerReturn);
        return SUCCESS_TIP;
    }

    /**
     * 客户退货详情
     */
    @RequestMapping(value = "/detail/{customerReturnId}")
    @ResponseBody
    public Object detail(@PathVariable("customerReturnId") Integer customerReturnId) {
        return customerReturnService.selectById(customerReturnId);
    }
}
